<?php

/**
 * Created by PhpStorm.
 * User: fenfen
 * Date: 2016/10/17
 * Time: 18:30
 */
namespace backend\controllers;

use bc\controller\BackendController;
use bc\model\PrizeinfoModel;
use bc\model\GradeModel;
use bc\model\SpreadModel;
use yii;

class PrizeinfoController extends BackendController
{
	/**
	 * 我的兑奖
	 */
	public function actionIndex()
	{
		$id=$_REQUEST['id'];
		$spreadM = new SpreadModel();
		$spreadM->columns = 'spread_id,openid,name,headimgurl,grade_id';
		$spreadM->autoId = 'spread_id';
		$spreadList = $spreadM->getRowById($id);

		//获取我的兑奖明细
		$where = array (
			'openid' =>$spreadList['openid']
		);

		$prizeinfoM = new PrizeinfoModel();
		$prizeinfoList = $prizeinfoM->getList($where);
		$row ['prizeinfoList'] = $prizeinfoList;
		$this->setGrade($spreadList);
		$row ['spreadList'] = $spreadList;
		$row['id'] = $id;
//		print_r($row);
//		die();

		return $this->render('index',$row);

	}

	/*
	 * 设置等级信息,兑奖按钮信息
	 * */
	 private function setGrade(&$spreadList)
	 {
		 $gradeM = new GradeModel();
		 $gradeM->columns = 'id,grade_id,grade_cn,min_num,prize';
		 $where=array(
			'grade_id'=>$spreadList['grade_id'],
		 );
		 $gradeList = $gradeM->getRow($where);

		 $spreadList['grade_cn'] = empty($gradeList) ? '无兑奖' : $gradeList['grade_cn'];

		//获取是否按钮信息
		 $sql = "select * from bc_grade where  grade_id = ( select max(grade_id) from bc_grade where min_num <=".$spreadList['success_num'].")" ;
		 $buttonrow = $gradeM->getDb ()->createCommand ( $sql )->queryOne ();

		 if(!empty($buttonrow)){
				//判断等级是否比当前等级大
			    $spreadList['grade_button'] = ($buttonrow['grade_id'] > $gradeList['grade_id']) ? '点击兑奖到'. $buttonrow['grade_cn'] : '';
		 }else{
			 $spreadList['grade_button']='';
		 }
		 $spreadList['grade_button_id']=$buttonrow['grade_id'];
	 }

	/*
	 * 兑奖
	 * */
	public function actionGrade()
	{

		$rdate = array('result'=>0);
		$spreadid = $this->get('spreadid', '');
		$gradeid = $this->get('gradeid', '');
		if(empty($spreadid) || empty($gradeid) ){
			$rdate['messge'] = '错误';
			echo json_encode($rdate);
			die();
		}
		/*
		 *兑奖
		 *1.检查当前成功推荐次数是否达到要兑奖等级
		 *2.开启事务
		 *3.将推荐人兑奖等级修改到当前等级
		 *4.入兑奖日志表
		 *5.提交事务
 		 */

		//1.检查当前成功推荐次数是否达到要兑奖等级
		//获取要获取当前信息
		$gredeM = new GradeModel();
		$where_grede = array(
			'grade_id' => $gradeid,
		);
		$gredeRow = $gredeM->getRow($where_grede);

		//获取用户当前信息
		$spreadM = new SpreadModel();
		$where_spread = array(
			'spread_id' => $spreadid,
		);
		$spreadRow = $spreadM->getRow($where_spread);

		if(empty($gredeRow) || empty($spreadRow)){
			$rdate['messge'] = '用户或等级数据空';
			echo json_encode($rdate);
			die();
		}

		///判断当前成功推荐数是否大于等级要求数
		if($spreadRow['success_num'] < $gredeRow['min_num']){
			$rdate['messge'] = '推荐次数不够兑奖，请刷新重试';
			echo json_encode($rdate);
			die();
		}
		$this->setGradeAndData($rdate,$gredeM,$spreadM,$spreadid,$gradeid,$gredeRow,$spreadRow);
        $rdate['result']= 1;
        echo json_encode($rdate);
	}

	//兑奖事务
	private function setGradeAndData($rdate,$gredeM,$spreadM,$spreadid,$gradeid,$gredeRow,$spreadRow)
	{
        $prizeinfoM = new PrizeinfoModel();

		$session = new yii\web\Session();

		$user=$session->get('admin');
		$spreadD=array(
			'grade_id' => $gradeid,
		);

		//2.开启事务
		$transaction= Yii::$app->db->beginTransaction();//创建事务
		try {
			//3.将推荐人兑奖等级修改到当前等级
			$spreadM->modify($spreadD,$spreadid);

			//4.入兑奖日志表
			$gredeD['openid']=$spreadRow['openid'];
			$gredeD['operator']=$user['account'];
			$gredeD['create_time']=time();
			$gredeD['grade']=$gradeid;
			$gredeD['success_num']=$spreadRow['success_num'];
			$gredeD['prize']=$gredeRow['prize'];
            $prizeinfoM->add ( $gredeD );
			//5.提交事务
			$transaction->commit();//提交事务
		} catch (Exception $e) {
			$rdate['messge'] = '兑奖失败';
			$transaction->rollback();//回滚事务
			die();
		}
	}

}





